﻿/***************************************************************
 * 开发人员：网魂小兵@http://www.mozlite.com
 * 
 * 控制面板网页基类。
 * 
 * 最后修改：2011-10-30
 * ************************************************************/
namespace Mozlite.Web
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Mozlite.Common;
    using System.Web.Mvc;
    using Mozlite.FileStorages;

    /// <summary>
    /// 控制面板控制器基类。
    /// </summary>
    [UCAuthorize]
    public abstract class UCenterBaseController:WebBaseController<UCenterApplication, UCenterConfiguration>
    {
        private UCenterPermission permission;
        /// <summary>
        /// 获取当前用户权限。
        /// </summary>
        public UCenterPermission Permission {
            get {
                if (permission == null)
                    permission = Permissions.GetPermission<UCenterPermission>();
                return permission;
            }
        }

        /// <summary>
        /// 删除文件夹。
        /// </summary>
        /// <param name="did">文件夹ID。</param>
        /// <returns>返回行为结果。</returns>
        protected ActionResult DeleteFolder(int did) {
            UserFolder dFolder;
            if (Storages.GetFolders().TryGetValue(did, out dFolder))
            {
                if (dFolder.IsEmpty)
                {
                    if (Storages.DeleteFolder(did))
                        return Json(new { status = true, message = Local("Folder.Delete.Success") });
                }
                else
                    return Json(new { status = false, message = Local("Folder.Delete.NotEmpty") });
            }
            return Json(new { status = false, message = Local("Folder.Delete.Error") });
        }

        /// <summary>
        /// 删除文件。
        /// </summary>
        /// <param name="did">文件ID。</param>
        /// <returns>返回行为结果。</returns>
        protected ActionResult DeleteFile(int did) {
            if (Storages.DeleteFile(did))
                return Json(new { status = true, message = Local("UserFile.Delete.Success") });
            return Json(new { status = false, message = Local("UserFile.Delete.Error") });
        }

        /// <summary>
        /// 上传文件。
        /// </summary>
        /// <param name="folder">当前文件夹。</param>
        /// <param name="formName">表单名称。</param>
        /// <returns>返回行为结果。</returns>
        protected ActionResult UploadFile(UserFolder folder, string formName = "UploadFile")
        {
            var postedFile = Request.Files[formName];
            if (postedFile != null && postedFile.ContentLength > 0)
            {
                if (folder == null)
                    folder = new UserFolder();
                var file = new UserFile(postedFile, folder.FolderID);
                if (!folder.IsNameValided(file.FullName))
                {
                    SetResourceStatus(FileStorageStatus.DuplicateFileName, true);
                    return View(folder);
                }
                if (!Permission.IsFileAllowed(file.Extension))
                {
                    SetResourceStatus(FileStorageStatus.InvalidExtension, true, Permission.AllowableExtensions);
                    return View(folder);
                }
                if (file.ContentLength > Permission.MaxSingleFileLength)
                {
                    SetResourceStatus(FileStorageStatus.SingleFileSizeError, true, Permission.MaxSingleFileLength.ToContentLengthString());
                    return View(folder);
                }
                if (Storages.GetDiskUsage() + file.ContentLength > Permission.MaxDiskSize)
                {
                    SetResourceStatus(FileStorageStatus.DiskFileSizeError, true, Permission.MaxDiskSize.ToContentLengthString());
                    return View(folder);
                }
                file.SaveAsTemp();
                FileStorageStatus status = Storages.SaveFile(file);
                if (status == FileStorageStatus.Created)
                {
                    file.Completed();
                    folder.Refresh();
                }
                SetResourceStatus(status, status != FileStorageStatus.Created);
            }
            return View(folder);
        }
    }
}
